package com.mytaobao.main.firsttab.func.flashsale;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.imageload.ImageLoaderUtil;
import com.baseproject.util.UIUtils;
import com.mytaobao.R;
import com.tmall.wireless.tangram.structure.BaseCell;
import com.tmall.wireless.tangram.structure.view.ITangramViewLifeCycle;

import static com.baseproject.helper.ViewHelper.setPaddingFromCell;

/**
 * 淘抢购等子item组件，上下布局
 */
public class FlashSaleViewTB extends LinearLayout implements ITangramViewLifeCycle{

    protected ImageView ivTopTitleIcon;
    protected TextView tvTopTitle;
    protected ImageView ivLeftImage;
    protected ImageView ivRightImage;
    protected TextView tvMiddeleTitle;

    public FlashSaleViewTB(Context context) {
        super(context);
        init(context);
    }

    public FlashSaleViewTB(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FlashSaleViewTB(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LinearLayout.LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT, (float)0.5);
        lp.weight = (float)0.5;
        lp.leftMargin = UIUtils.dip2px(10);//这里设置不起作用，放到构造本类的地方生效
        lp.topMargin = UIUtils.dip2px(5);

        setLayoutParams(lp);
        this.setOrientation(VERTICAL);

        LayoutInflater.from(context).inflate(R.layout.sub_item_flash_sale_tb, this, true);
        setBackgroundColor(getResources().getColor(R.color.ccc));

        ivTopTitleIcon = (ImageView) this.findViewById(R.id.top_title_icon);
        tvTopTitle = (TextView) this.findViewById(R.id.top_title);
        tvMiddeleTitle = (TextView) this.findViewById(R.id.middle_title);
        ivLeftImage = (ImageView) this.findViewById(R.id.left_image);
        ivRightImage = (ImageView) this.findViewById(R.id.right_image);
    }

    @Override
    public void cellInited(BaseCell cell) {

    }

    @Override
    public void postBindView(BaseCell cell) {
        ImageLoaderUtil.getInstance().with(getContext()).load(cell.optStringParam("topTitleIcon")).into(ivTopTitleIcon);
        tvTopTitle.setText(cell.optStringParam("toTitle"));
        tvMiddeleTitle.setText(cell.optStringParam("middleTitle"));
        ImageLoaderUtil.getInstance().with(getContext()).load(cell.optStringParam("leftImage")).into(ivLeftImage);
        ImageLoaderUtil.getInstance().with(getContext()).load(cell.optStringParam("rightImage")).into(ivRightImage);
        setPaddingFromCell(cell, this);
    }

    @Override
    public void postUnBindView(BaseCell cell) {

    }

}
